using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Collections;
using System.Drawing;
using System.Workflow.ComponentModel;
using System.Workflow.ComponentModel.Design;
using System.Workflow.ComponentModel.Compiler;
using System.Workflow.ComponentModel.Serialization;
using System.Workflow.Runtime;
using System.Workflow.Activities;
using System.Workflow.Activities.Rules;
using System.Drawing.Drawing2D;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Drawing.Design;
using OpenComposite.Workflow.Properties;
using System.Threading;

namespace OpenComposite.Workflow
{
	[Designer(typeof(LogicalAsyncStartActivityDesigner))]
	[ToolboxItem(typeof(LogicalAsyncStartActivityToolboxItem))]
	[ToolboxBitmap(typeof(resfinder), "OpenComposite.Workflow.Resources.logact_async_start.png")]
	public partial class LogicalAsyncStartActivity
		: LogicalCapabilityActivity, ITrackingActivity
	{
		#region Initialization
		public LogicalAsyncStartActivity()
		{
			InitializeComponent();

			this.Type = LogicalCapabilityType.AsyncStart;
		}
		#endregion

		#region AsyncState
		// Using a DependencyProperty as the backing store for AsyncState.  This enables animation, styling, binding, etc...
		public static readonly DependencyProperty AsyncStateProperty =
			DependencyProperty.Register("AsyncState", typeof(LogicalAsyncState),
										typeof(LogicalAsyncStartActivity), new PropertyMetadata(null));
#if DEBUG
		[Browsable(true)]
#else
		[Browsable(false)]
#endif
		public LogicalAsyncState AsyncState
		{
			get { return (LogicalAsyncState)GetValue(AsyncStateProperty); }
			set { SetValue(AsyncStateProperty, value); }
		}
		#endregion

		//#region Text
		//public static DependencyProperty TextProperty = DependencyProperty.Register(
		//    "Text", typeof(string), typeof(LogicalAsyncStartActivity), new PropertyMetadata(""));
		//[Editor(typeof(MultilineStringEditor), typeof(UITypeEditor))]
		//[DefaultValue("")]
		//[Browsable(true)]
		//public string Text
		//{
		//    get { return (string)base.GetValue(LogicalAsyncStartActivity.TextProperty); }
		//    set { base.SetValue(LogicalAsyncStartActivity.TextProperty, value); }
		//}
		//#endregion

		#region Event: AsyncReceived
		public static readonly DependencyProperty AsyncReceivedEvent =
			DependencyProperty.Register("AsyncReceived", typeof(EventHandler<AsyncReceivedEventArgs>),
				typeof(LogicalAsyncStartActivity));
		public event EventHandler<AsyncReceivedEventArgs> AsyncReceived
		{
			add { base.AddHandler(AsyncReceivedEvent, value); }
			remove { base.RemoveHandler(AsyncReceivedEvent, value); }
		}
		public void RaiseAsyncReceived()
		{
			base.RaiseEvent(AsyncReceivedEvent, this, AsyncReceivedEventArgs.Empty);
		}
		#endregion
	}

	#region Class: AsyncReceivedEventArgs : Base: EventArgs
	public class AsyncReceivedEventArgs : EventArgs
	{
		public new static AsyncReceivedEventArgs Empty = new AsyncReceivedEventArgs();
	}
	#endregion

	#region Class: LogicalAsyncStartActivityToolboxItem : Base: ActivityToolboxItem
	[Serializable]
	public class LogicalAsyncStartActivityToolboxItem : LogicalCapabilityActivityToolboxItem
	{
		public LogicalAsyncStartActivityToolboxItem(Type type)
			: base(type)
		{
			base.DisplayName = "Logical Async Start";
		}

		protected override IComponent[] CreateComponentsCore(IDesignerHost host)
		{
			LogicalAsyncStartActivity act = new LogicalAsyncStartActivity();

			OnInitializeLogicalCapabilityActivity(act);

			return new IComponent[] { act };
		}
	}
	#endregion

	#region Class: LogicalAsyncStartActivityDesigner : Base: LogicalCapabilityActivityDesigner
	[ActivityDesignerTheme(typeof(LogicalAsyncStartActivityDesignerTheme))]
	public class LogicalAsyncStartActivityDesigner : LogicalCapabilityActivityDesigner
	{
		public LogicalAsyncStartActivityDesigner()
		{
		}

		public override Image Image
		{
			get { return Resources.logact_async_start; }
			protected set { base.Image = value; }
		}
	}
	#endregion

	#region Class: LogicalAsyncStartActivityDesignerTheme : Base: ActivityDesignerTheme
	public class LogicalAsyncStartActivityDesignerTheme : LogicalCapabilityActivityDesignerTheme
	{
		public LogicalAsyncStartActivityDesignerTheme(WorkflowTheme theme)
			: base(theme)
		{
			base.Initialize();
			this.BorderStyle = DashStyle.Solid;
			this.BorderColor = Color.CornflowerBlue;
			this.BackColorStart = Color.White;
			this.BackColorEnd = Color.CornflowerBlue;
			this.BackgroundStyle = LinearGradientMode.Vertical;
			this.ForeColor = Color.Black;
		}
	}
	#endregion

	#region Public Class: LogicalAsyncState
	public class LogicalAsyncState
	{
		public LogicalAsyncState(
			WorkflowQueuingService workflowQueuingService,
			string workflowQueueName,
			Guid workflowInstanceId)
		{
			this.WorkflowQueuingService = workflowQueuingService;
			this.WorkflowQueueName = workflowQueueName;
			this.WorkflowInstanceId = workflowInstanceId;
			this.Output = null;
			this.UserState = null;
			this.IsReceived = false;
			this.WorkflowInstance = null;
			this.AsyncResult = null;
		}

		public WorkflowInstance WorkflowInstance { get; set; }
		public IAsyncResult AsyncResult { get; set; }
		public WorkflowQueuingService WorkflowQueuingService { get; set; }
		public string WorkflowQueueName { get; set; }
		public Guid WorkflowInstanceId { get; set; }
		public object Output { get; set; }
		public object UserState { get; set; }
		public bool IsReceived { get; set; }
		public object Client { get; set; }

	}
	#endregion
}
